
import random
import os

def get_Time_string(hour, minute):
	am_pm = 'am'
	if(hour > 11): am_pm = 'pm'
	if(hour > 12): hour = hour - 12

	H = str(hour)
	M = str(minute)
	if len(M) == 1: M = '0' + M

	return H + ':' + M + am_pm


def getRandomStr(leng, prePath, ext):
	rand_str = ''
	for i in range(leng): rand_str += random.choice('abcdefghijklmnopqrstuvABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')
	return prePath + rand_str + ext


def toUTF8(valStr):
	return unicode(valStr).encode("utf-8")

def read_file(file_name):
	fin = open(file_name,'r')
	output = fin.read()
	fin.close()
	return output

def mail(to, subject, text, account_name, account_pswd, email_source, attachment_source):
	subject = subject.replace(' ','kathia')
	word = getRandomStr(10, '', '') 
	to = to.replace(' ','')

	fin = open('src/tmpMailMsg/' + word, 'w')
	fin.write(toUTF8(text))
	fin.close()
	
	emailCmd = 'python2.7 ./src/mail.py ' + to + ' ' + subject + ' ' + word + ' ' + 'chieh'
	emailCmd += ' ' + account_pswd + ' ' + email_source + ' ' + attachment_source + ' &'
	print emailCmd
	os.system(emailCmd)


def specialToSafe(cString):
	cString = cString.replace(",", '|com|')
	cString = cString.replace("'", '|===|')
	cString = cString.replace("&", "|amps|") 
	cString = cString.replace("#", "|pnd|") 
	cString = cString.replace("@", "|at|") 
	cString = cString.replace("^", "|hat|") 
	cString = cString.replace("%", "|pcnt|") 
	cString = cString.replace("(", "|lbr|") 
	cString = cString.replace(")", "|rbr|") 
	cString = cString.replace("*", "|star|") 
	cString = cString.replace("[", "|lbra|") 
	cString = cString.replace("]", "|rbra|") 
	cString = cString.replace("?", "|qst|") 
	cString = cString.replace(":", "|col|") 
	cString = cString.replace(";", "|scol|") 
	cString = cString.replace("{", "|lcbr|") 
	cString = cString.replace("}", "|rcbr|") 
	cString = cString.replace("\"", "|dqu|") 
	#cString = cString.replace("/", "|slash|") 
	return cString


def SafeStringToSpecial(cString):
	cString = cString.replace('|===|',"'")
	cString = cString.replace("|amps|",   "&") 
	cString = cString.replace("|pnd|",    "#") 
	cString = cString.replace("|at|",     "@") 
	cString = cString.replace("|hat|",    "^") 
	cString = cString.replace("|pcnt|",   "%") 
	cString = cString.replace("|lbr|",   "(") 
	cString = cString.replace("|rbr|",   ")") 
	cString = cString.replace("|star|",   "*") 
	cString = cString.replace("|lbra|",   "[") 
	cString = cString.replace("|rbra|",   "]") 
	cString = cString.replace("|qst|",   "?") 
	cString = cString.replace("|col|",   ":") 
	cString = cString.replace("|scol|",   ";") 
	cString = cString.replace('|com|', ",")
	cString = cString.replace('|lcbr|', "{")
	cString = cString.replace('|rcbr|', "}")
	cString = cString.replace('|dqu|', "\"")
	cString = cString.replace('|nl|', "\n")

	return cString
	
